//
//  ZBShareSDKView.m
//  ZBShareSDKDM
//
//  Created by apple on 2018/10/2.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "ZBShareSDKView.h"
#import "ZBContentVerticalView.h"

#define ZBShareSDKView_SCREEN_WIDTH        [UIScreen mainScreen].bounds.size.width
#define ZBShareSDKView_SCREEN_HEIGHT       [UIScreen mainScreen].bounds.size.height
#define ZBShareSDKView_DEFAULT_TAG 100

//尺寸
#define ZBShareSDKView_ButtonHeight  80
#define ZBShareSDKView_ButtonWidth  50
#define ZBShareSDKView_TopBottomMargin  20
#define ZBShareSDKView_headerHeight  40
#define ZBShareSDKView_cancleHeight  39.5
#define ZBShareSDKView_line_height 0.5

//分享数量
#define ZBShareSDKView_Count  5
//动画时间
#define ZBShareSDKViewDuration   0.2
@implementation ZBShareSDKViewConfig
+ (instancetype)defaultShareSDKViewConfig
{
    ZBShareSDKViewConfig *config =  [[ZBShareSDKViewConfig alloc]init];
    return config;
}
-(instancetype)init
{
    if(self = [super init])
    {
        self.separateLineColor = [UIColor redColor];
        
        self.cancleText = @"取消";
        self.cancleFont = [UIFont systemFontOfSize:14.0];
        self.cancleColor = [UIColor redColor];
        
        //默认间距
        self.defaultLRMargin = 20;
        
        NSMutableArray *temp = [NSMutableArray array];
        NSDictionary *dict_weixin = @{
                               @"name":@"微信好友",
                               @"image":@"login_wechat",
                               };
        NSDictionary *dict_qq = @{
                                      @"name":@"QQ",
                                      @"image":@"login_qq",
                                      };
        NSDictionary *dict_weibo = @{
                                      @"name":@"微博",
                                      @"image":@"login_weibo",
                                      };
        
        [temp addObject:dict_weixin];
        [temp addObject:dict_qq];
        [temp addObject:dict_weibo];
        self.buttonContents = temp;
    }
    return self;
}
@end


@interface ZBShareSDKView()
@property (nonatomic, strong)  UIView  *containerView;
@property (nonatomic, strong)  UIView  *header;
@property (nonatomic, strong)  UIView  *line;
@property (nonatomic, strong)  UIScrollView *scrollView;
@property (nonatomic, strong)  UIButton *cancelButton;

@property (nonatomic, copy)    targetBlock   eventBlock;
@property (nonatomic, strong)  ZBShareSDKViewConfig *configModel;
@end

@implementation ZBShareSDKView

#pragma mark - puplic
+ (void)showWithCompeletion:(targetBlock)compeletion
{
    [self showWithConfig:[ZBShareSDKViewConfig defaultShareSDKViewConfig] compeletion:compeletion];
}
+ (void)showWithConfig:(ZBShareSDKViewConfig *)config compeletion:(targetBlock)compeletion;
{
    [self showInView:nil config:config targetEventCompeletion:compeletion];
}

+ (void)showInView:(UIView *)superView  config:(ZBShareSDKViewConfig *)config targetEventCompeletion:(targetBlock)compeletion;
{
    if(superView == nil){
        //黑色蒙版
        superView = [UIApplication sharedApplication].keyWindow;
    }
    ZBShareSDKView *shareView = [[ZBShareSDKView alloc]init];
    [superView addSubview:shareView];
    shareView.frame = [UIScreen mainScreen].bounds;
    shareView.alpha = 0.0;
    shareView.eventBlock = compeletion;
    shareView.configModel = config;
    
    
    //新增子空间
    NSInteger count = config.buttonContents.count;
    for (NSInteger i = 0; i < count; i++) {
      
        NSDictionary *dict = config.buttonContents[i];
        
        
        NSString *title = [dict valueForKey:@"name"];
        NSString *imageName = [dict valueForKey:@"image"];
        UIImage *image = [UIImage imageNamed:imageName];
        if(image == nil){
            image = [UIImage imageWithContentsOfFile:imageName];
        }
    
        ZBContentVerticalView *button = [[ZBContentVerticalView alloc]initWithImage:image title:title];
        button.tag = ZBShareSDKView_DEFAULT_TAG + i;
        [button addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:shareView action:@selector(buttonClickEvent:)]];
        [shareView.scrollView addSubview:button];

    }
    
    [UIView animateWithDuration:ZBShareSDKViewDuration animations:^{
        shareView.alpha = 1.0;
    }completion:^(BOOL finished) {
        [UIView animateWithDuration:ZBShareSDKViewDuration animations:^{
            CGRect frame = shareView.containerView.frame;
            frame.origin.y = ZBShareSDKView_SCREEN_HEIGHT - shareView.containerView.frame.size.height;
            shareView.containerView.frame = frame;
        }];
        
    }];
    
}



#pragma mark - 事件
- (void)cancleBottomAlterView
{
     [self cancelButtonClick];
}

- (void)cancelButtonClick
{
    [UIView animateWithDuration:ZBShareSDKViewDuration animations:^{
        CGRect frame = self.containerView.frame;
        frame.origin.y = ZBShareSDKView_SCREEN_HEIGHT;
        self.containerView.frame = frame;
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.0];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)buttonClickEvent:(UITapGestureRecognizer *)tap
{
    NSInteger index = tap.view.tag - ZBShareSDKView_DEFAULT_TAG;
    if(index < self.configModel.buttonContents.count){
         if(self.eventBlock)
             self.eventBlock(index);
        [self cancelButtonClick];
    }
}


- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
        [self addSubview:self.containerView];
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancleBottomAlterView)]];
        [self.containerView addSubview:self.scrollView];
        [self.containerView addSubview:self.header];
        [self.header addSubview:self.cancelButton];
        [self.header addSubview:self.line];

    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    //高度计算
    CGFloat allHeight = ZBShareSDKView_TopBottomMargin + ZBShareSDKView_ButtonHeight + ZBShareSDKView_TopBottomMargin + ZBShareSDKView_headerHeight;
    
    self.containerView.frame = CGRectMake(0, ZBShareSDKView_SCREEN_HEIGHT, ZBShareSDKView_SCREEN_WIDTH, allHeight);
    
    //scroollView
    CGFloat scroollViewX = 0;
    CGFloat scroollViewY = ZBShareSDKView_TopBottomMargin;
    CGFloat scroollViewW = ZBShareSDKView_SCREEN_WIDTH;
    CGFloat scroollViewH = ZBShareSDKView_ButtonHeight;
    self.scrollView.frame = CGRectMake(scroollViewX, scroollViewY, scroollViewW, scroollViewH);
    
    
    //按钮子控件
    CGFloat buttonW = ZBShareSDKView_ButtonWidth;
    CGFloat buttonH = ZBShareSDKView_ButtonHeight;
    CGFloat buttonX = 0;
    CGFloat buttonY = 0;
    
    NSInteger count = self.configModel.buttonContents.count;
    if(count <= ZBShareSDKView_Count){
       
        CGFloat buttonMargin = (scroollViewW - buttonW *count)/(count + 1);
        for (NSInteger i = 0; i < ZBShareSDKView_Count; i++) {
            buttonX = (buttonMargin + buttonW)  * i + buttonMargin;
            
            ZBContentVerticalView *button = [self.scrollView viewWithTag:ZBShareSDKView_DEFAULT_TAG + i];
            button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
        }
    }else{
       
        CGFloat contentsizeWidth =  2 *self.configModel.defaultLRMargin + buttonW *count + self.configModel.defaultLRMargin*(count-1);
        self.scrollView.contentSize = CGSizeMake(contentsizeWidth, 0);
        for (NSInteger i = 0;i < count; i++)
        {
            CGFloat buttonX = self.configModel.defaultLRMargin + i*(buttonW + self.configModel.defaultLRMargin);
            ZBContentVerticalView *button = [self.scrollView viewWithTag:ZBShareSDKView_DEFAULT_TAG + i];
            button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
        }
        
    }
    
    
    //布局取消view
    self.header.frame = CGRectMake(0, CGRectGetMaxY(self.scrollView.frame) +ZBShareSDKView_TopBottomMargin , scroollViewW, ZBShareSDKView_headerHeight);
    self.line.frame = CGRectMake(0, 0, self.header.frame.size.width, ZBShareSDKView_line_height);
    self.cancelButton.frame = CGRectMake(0, CGRectGetMaxY(self.line.frame), self.header.frame.size.width, ZBShareSDKView_headerHeight - ZBShareSDKView_line_height);
    
    
    
}

- (void)setConfigModel:(ZBShareSDKViewConfig *)configModel
{
    _configModel = configModel;
   
    [self.cancelButton setTitleColor:configModel.cancleColor forState:UIControlStateNormal];
    [self.cancelButton.titleLabel setFont:configModel.cancleFont];
    [self.cancelButton setTitle:configModel.cancleText forState:UIControlStateNormal];
    
    self.line.backgroundColor = configModel.separateLineColor;

    
}




#pragma mark - lazy
- (UIView *)containerView
{
    if(_containerView == nil)
    {
        _containerView = [UIView new];
        _containerView.backgroundColor = [UIColor whiteColor];
    }
    return _containerView;
}
- (UIView *)header
{
    if(_header == nil)
    {
        _header = [UIView new];
        _header.backgroundColor = [UIColor whiteColor];
    }
    return _header;
}
- (UIButton *)cancelButton
{
    if(_cancelButton == nil)
    {
        _cancelButton = getShareSDKViewButtonWithText(@"取消", self, @selector(cancelButtonClick));
    }
    return _cancelButton;
}
- (UIScrollView *)scrollView
{
    if(_scrollView == nil)
    {
        _scrollView = [UIScrollView new];
        _scrollView.showsHorizontalScrollIndicator = NO;
    }
    return _scrollView;
}

- (UIView *)line
{
    if(_line == nil)
    {
        _line = getShareSDKTopLine();
    }
    return _line;
}

UIButton *getShareSDKViewButtonWithText(NSString *text, id target, SEL action){
    UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
    [b setTitle:text forState:UIControlStateNormal];
    [b setBackgroundColor:[UIColor whiteColor]];
    [b setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    b.titleLabel.font = [UIFont systemFontOfSize:14.0];
    [b addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return b;
}

UIView *getShareSDKTopLine()
{
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ZBShareSDKView_SCREEN_WIDTH, ZBShareSDKView_line_height)];
    line.backgroundColor = [UIColor grayColor];
    return line;
}
@end
